package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.R;
import io.renren.service.MachineService;
import io.renren.service.MemberMachineService;
import io.renren.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 矿机相关
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/machine")
@Api(tags = "矿机接口")
public class ApiMachineController extends AbstractController {

	@Autowired
	private MachineService machineService;

	@Autowired
	private MemberMachineService memberMachineService;

	@Autowired
	MemberService memberService;

	@Login
	@PostMapping("list")
	@ApiOperation("矿机列表")
	public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageUtils page = machineService.queryPage(params);
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("info")
	@ApiOperation("矿机详情")
	public R info(@RequestParam("machineId") Long machineId) {
		return R.ok().put("machine", machineService.selectById(machineId));
	}

	@Login
	@PostMapping("buy")
	@ApiOperation("购买矿机")
	public R buy(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam("machineId") Long machineId) {
		memberMachineService.buy(userId, machineId);
		return R.ok();
	}

	@Login
	@PostMapping("myMachine")
	@ApiOperation("我的矿机")
	public R myMachine(@ApiIgnore @RequestAttribute("userId") Long userId) {
		// 个人算力
		Double totalHashSecond = memberMachineService.totalHashSecond(userId);
		totalHashSecond = totalHashSecond == null ? 0 : totalHashSecond;
		// 个人运行中的矿机每日收益
		Double totalPerDayProfit = memberMachineService.totalPerDayProfit(userId);
		totalPerDayProfit = totalPerDayProfit == null ? 0 : totalPerDayProfit;
		// 运行中的矿机分页
		Object page = myRunningList(userId, new HashMap<String, Object>()).get("page");
		return R.ok().put("totalHashSecond", totalHashSecond).put("totalPerDayProfit", totalPerDayProfit).put("page", page);
	}

	@Login
	@PostMapping("getProfit")
	@ApiOperation("领取收益")
	public R getProfit(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestParam("mmId") Long mmId) {
		memberMachineService.getProfit(userId, mmId);
		return R.ok();
	}

	@Login
	@PostMapping("myRunningList")
	@ApiOperation("运行中的矿机")
	public R myRunningList(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("userId", userId);
		params.put("status", new Integer[] { ProjectConstant.MACHINE_RUNSTATUS_RUNNING });
		PageUtils page = memberMachineService.queryPage(params);
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("myFinishList")
	@ApiOperation("已停止的矿机")
	public R myFinishList(@ApiIgnore @RequestAttribute("userId") Long userId, @ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("userId", userId);
		params.put("status", new Integer[] { ProjectConstant.MACHINE_RUNSTATUS_PAUSE, ProjectConstant.MACHINE_RUNSTATUS_FINISH });
		PageUtils page = memberMachineService.queryPage(params);
		return R.ok().put("page", page);
	}
}
